/**
 ******************************************************
 * 作者:朱雍
 * 备注:
 * 时间:2017年3月14日下午3:46:56
 * Copyright (c) 2017, 243813208@qq.com All Rights Reserved.
 ******************************************************
 */

package cn.sxt.yxl.users.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.sxt.yxl.common.util.PageInfoUtil;
import cn.sxt.yxl.users.dao.IAAdminsDao;
import cn.sxt.yxl.users.dao.IARoleDao;
import cn.sxt.yxl.users.pojo.AAdmins;
import cn.sxt.yxl.users.pojo.ARole;
import cn.sxt.yxl.users.service.IUserService;

/**
 * 类名:UserService 
 * 备注:	 
 * 时间:2017年3月14日 下午3:46:56
 * 作者:朱雍
 * @since  	JDK 1.8	 
 */
@Service("userService")
public class UserServiceImpl implements IUserService{
	@Resource
	private IAAdminsDao adminsDao;
	@Resource
	private IARoleDao roleDao;
	//查询单条管理员
	@Override
	public AAdmins findOneAdmin(Map<String, Object> condMap) {
		
		return this.adminsDao.findOne(condMap);
	}
	//添加一条管理员
	@Override
	public JSONObject saveOneAdmin(AAdmins admins) {
		
		JSONObject resultJSON = new JSONObject();
		int res = this.adminsDao.saveOne(admins);
		if(res > 0){
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", admins.getId());
			dataJSON.put("effectCount", res);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			resultJSON.put("data", dataJSON);
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}
	
	//关键字搜索
	@Override
	public List<AAdmins> findCondAdminsList(PageInfoUtil pageInfoUtil,Map<String, Object> condMap) {
		if(pageInfoUtil != null){
			//分页
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<AAdmins> list = this.adminsDao.findCondList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() +""));
			return list;
		}
		return this.adminsDao.findCondList(condMap);
	}
	
	//更新一条记录
	@Override
	public JSONObject updateOneAAdmin(AAdmins admins) {
		
		JSONObject resultJSON = new JSONObject();
		int res = this.adminsDao.updateOne(admins);
		if(res > 0){
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", admins.getId());
			dataJSON.put("effectCount", res);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			resultJSON.put("data", dataJSON);
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}
	@Override
	public JSONObject deleteOneAdmin(AAdmins admins) {
		
		JSONObject resultJSON = new JSONObject();
		int res = this.adminsDao.deleteOne(admins);
		if(res > 0){
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", admins.getId());
			dataJSON.put("effectCount", res);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			resultJSON.put("data", dataJSON);
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}

	
	//查询单条角色
	@Override
	public ARole findOneRole(Map<String, Object> condMap) {
		
		return this.roleDao.findOne(condMap);
	}
	//添加一条角色
	@Override
	public JSONObject saveOneRole(ARole role) {
		
		JSONObject resultJSON = new JSONObject();
		int res = this.roleDao.saveOne(role);
		if(res > 0){
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", role.getId());
			dataJSON.put("effectCount", res);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			resultJSON.put("data", dataJSON);
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}
	
	//关键字搜索
	@Override
	public List<ARole> findCondRoleList(PageInfoUtil pageInfoUtil,Map<String, Object> condMap) {
		if(pageInfoUtil != null){
			//分页
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<ARole> list = this.roleDao.findCondList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() +""));
			return list;
		}
		return this.roleDao.findCondList(condMap);
	}
	
	//更新一条记录
	@Override
	public JSONObject updateOneARole(ARole role) {
		
		JSONObject resultJSON = new JSONObject();
		int res = this.roleDao.updateOne(role);
		if(res > 0){
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", role.getId());
			dataJSON.put("effectCount", res);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			resultJSON.put("data", dataJSON);
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}
	@Override
	public JSONObject deleteOneRole(ARole role) {
		
		JSONObject resultJSON = new JSONObject();
		int res = this.roleDao.deleteOne(role);
		if(res > 0){
			JSONObject dataJSON = new JSONObject();
			dataJSON.put("id", role.getId());
			dataJSON.put("effectCount", res);
			resultJSON.put("code", "0");
			resultJSON.put("info", "成功");
			resultJSON.put("data", dataJSON);
		}else{
			resultJSON.put("code", "1");
			resultJSON.put("info", "失败");
		}
		return resultJSON;
	}
}

